
import { profile } from "console";
import { RoomModel } from "../../../../hall_server/model/RoomModel";
import { RoomTemplateModel } from "../../../../hall_server/model/RoomTemplateModel";
import { Config } from "../../../config/Config";
import { ServerConfig } from "../../../config/ServerConfig";
import { JhaoProto } from "../../../lib/net/protobuf/jhaoproto";
import { ProtoManager } from "../../../lib/net/protobuf/ProtoManager";
import { Logger } from "../../utils/Logger";
import { SocketConnection } from "./SocketConnection";
import { SocketConnectionManager } from "./SocketConnectionManager";
import { SocketMsg } from "./SocketMsg";

export class SocketClientMsgHandler {

    public static onEventSocketGata(socketConnect: SocketConnection, msg: JhaoProto.BaseMsg) {
        Logger.log("SocketClientMsgHandler======", socketConnect.isLogin(), msg)
        if (msg.cmdIndex == JhaoProto.CmdIndex.server_internal_Login) {
            this.server_internal_Login(socketConnect, msg)
        }
        else if (socketConnect.isLogin()) {
            if (msg.cmdIndex == JhaoProto.CmdIndex.server_internal_kill_all_users) {
                this.server_internal_kill_all_users(socketConnect, msg)
            }
            else if (msg.cmdIndex == JhaoProto.CmdIndex.server_internal_set_server_status) {
                this.server_internal_set_server_status(socketConnect, msg)
            }
            else if (msg.cmdIndex == JhaoProto.CmdIndex.server_internal_get_server_status) {
                this.server_internal_get_server_status(socketConnect, msg)
            }
            else if (msg.cmdIndex == JhaoProto.CmdIndex.server_internal_update_online) {
                this.server_internal_update_online(socketConnect, msg)
            }
            else if (msg.cmdIndex == JhaoProto.CmdIndex.server_internal_is_in_server) {
                this.server_internal_is_in_server(socketConnect, msg)
            }
            else if (msg.cmdIndex == JhaoProto.CmdIndex.server_internal_club_create_room_template) {
                this.server_internal_club_create_room_template(socketConnect, msg)
            }
            else if (msg.cmdIndex == JhaoProto.CmdIndex.server_internal_club_delete_room_template) {
                this.server_internal_club_delete_room_template(socketConnect, msg)
            }
            else if (msg.cmdIndex == JhaoProto.CmdIndex.server_internal_createRoom) {
                this.server_internal_createRoom(socketConnect, msg)
            }

        } else {
            socketConnect.sendErrorToClient(0, JhaoProto.ErrorCodeWs.LoginError_NoLogin, "账号未登录")
        }
        // else if (msg.protoName == ProtoServerConfig.req_server_onlie_club) {
        //     this.on_req_server_onlie_club(socketConnect, msg)
        // }
        // else if (msg.protoName == ProtoServerConfig.req_server_notify_delete_club_user) {
        //     this.on_req_server_notify_delete_club_user(socketConnect, msg)
        // }
        // else if (msg.protoName == ProtoServerConfig.req_server_notify_set_club_quanxian) {
        //     this.req_server_notify_set_club_quanxian(socketConnect, msg)
        // }
        // else if (msg.protoName == ProtoServerConfig.req_server_notify_club_freeze) {
        //     this.req_server_notify_club_freeze(socketConnect, msg)
        // }
        // else if (msg.protoName == ProtoServerConfig.req_server_notify_set_club_admin) {
        //     this.req_server_notify_set_club_admin(socketConnect, msg)
        // }

        // else if (msg.protoName == ProtoServerConfig.req_server_joinRoom) {
        //     this.on_req_server_joinRoom(socketConnect, msg)
        // }
        // else if (msg.protoName == ProtoServerConfig.req_server_deleteRoom) {
        //     this.on_req_server_deleteRoom(socketConnect, msg)
        // }
        // else if (msg.protoName == ProtoServerConfig.req_server_leave_room) {
        //     this.on_req_server_leave_room(socketConnect, msg);
        // }
        // else if (msg.protoName == ProtoServerConfig.req_server_notify_game_start) {
        //     this.on_req_server_notify_game_start(socketConnect, msg)
        // }
        // else if (msg.protoName == ProtoServerConfig.req_server_notify_update_gems) {
        //     this.req_server_notify_update_gems(socketConnect, msg)
        // }
        // else if (msg.protoName == ProtoServerConfig.req_server_notify_update_coins) {
        //     this.req_server_notify_update_coins(socketConnect, msg)
        // }
        // else if (msg.protoName == ProtoServerConfig.req_server_notify_update_income) {
        //     this.req_server_notify_update_income(socketConnect, msg)
        // }
        // else if (msg.protoName == ProtoServerConfig.req_server_notify_update_meili) {
        //     this.req_server_notify_update_meili(socketConnect, msg)
        // }
        // else if (msg.protoName == ProtoServerConfig.req_server_notify_update_lv) {
        //     this.req_server_notify_update_lv(socketConnect, msg)
        // }
        // else if (msg.protoName == ProtoServerConfig.req_server_notify_update_invite) {
        //     this.req_server_notify_update_invite(socketConnect, msg)
        // }
        // else if (msg.protoName == ProtoServerConfig.req_server_notify_update_img) {
        //     this.req_server_notify_update_img(socketConnect, msg);
        // }
        // else if (msg.protoName == ProtoServerConfig.req_server_notify_club_open_or_close) {
        //     this.req_server_notify_club_open_or_close(socketConnect, msg);
        // }
        // else if (msg.protoName == ProtoServerConfig.req_server_delete_room_by_hall) {
        //     this.req_server_delete_room_by_hall(socketConnect, msg)
        // }

    }

    private static server_internal_Login(conn: SocketConnection, socketMsg: JhaoProto.BaseMsg) {
        let protoPack: JhaoProto.req_server_internal_Login = ProtoManager.instance.decode(JhaoProto.req_server_internal_Login, socketMsg.data)
        if (protoPack.pass != "sjh666666") {
            conn.sendErrorToClient(0, JhaoProto.ErrorCodeWs.LoginError_NoPass, "密码错误")
            conn.gotoClose()
        } else {
            for (const key in Config.ServerType_List) {
                let value: string = Config.ServerType_List[key]
                if (protoPack.account.includes(value)) {
                    conn.isInternalServer = true
                    conn.account = protoPack.account
                    conn.sendErrorToClient(socketMsg.cmdOrder, JhaoProto.ErrorCodeWs.Suc, "", socketMsg.cmdIndex)
                    break
                }
            }
        }
    }

    private static server_internal_kill_all_users(conn: SocketConnection, socketMsg: JhaoProto.BaseMsg) {
        SocketConnectionManager.instance.killAllByWeihu()
    }

    private static server_internal_set_server_status(conn: SocketConnection, socketMsg: JhaoProto.BaseMsg) {
        let protoPack: JhaoProto.req_server_internal_set_server_status = ProtoManager.instance.decode(JhaoProto.req_server_internal_set_server_status, socketMsg.data)
        ServerConfig.nowServerConfig.status = protoPack.status;
    }
    private static server_internal_get_server_status(conn: SocketConnection, socketMsg: JhaoProto.BaseMsg) {
        let respData: JhaoProto.Iresp_server_internal_get_server_status = {
            status: ServerConfig.nowServerConfig.status
        }
        Logger.log("server_internal_get_server_status", respData)
        conn.createMsgToClient(socketMsg.cmdIndex, socketMsg.cmdOrder, JhaoProto.resp_server_internal_get_server_status, respData)
    }

    private static server_internal_update_online(conn: SocketConnection, socketMsg: JhaoProto.BaseMsg) {
        let protoPack: JhaoProto.req_server_internal_update_online = ProtoManager.instance.decode(JhaoProto.req_server_internal_update_online, socketMsg.data)
        ServerConfig.update_online(protoPack.serverId, protoPack.serverType, protoPack.userids)
    }

    private static server_internal_is_in_server(conn: SocketConnection, socketMsg: JhaoProto.BaseMsg) {
        let protoPack: JhaoProto.req_server_internal_is_in_server = ProtoManager.instance.decode(JhaoProto.req_server_internal_is_in_server, socketMsg.data)
        let isIn: boolean = false
        if (protoPack.account && protoPack.account != "") {
            SocketConnectionManager.instance.socketConnMap.forEach((conn: SocketConnection, key: number) => {
                if (conn.isLogin()) {
                    if (conn.account == protoPack.account) {
                        isIn = true
                        return
                    }
                }
            });
        }
        let respData: JhaoProto.Iresp_server_internal_is_in_server = {
            isIn: isIn
        }
        conn.createMsgToClient(socketMsg.cmdIndex, socketMsg.cmdOrder, JhaoProto.resp_server_internal_is_in_server, respData)
    }

    private static server_internal_club_create_room_template(conn: SocketConnection, socketMsg: JhaoProto.BaseMsg) {
        RoomTemplateModel.init()
    }

    private static server_internal_club_delete_room_template(conn: SocketConnection, socketMsg: JhaoProto.BaseMsg) {
        let protoPack: JhaoProto.req_server_internal_club_delete_room_template = ProtoManager.instance.decode(JhaoProto.req_server_internal_club_delete_room_template, socketMsg.data)
        RoomTemplateModel.deleteRoomTemplate(protoPack.id)
    }

    private static server_internal_createRoom(conn: SocketConnection, socketMsg: JhaoProto.BaseMsg) {
        let protoPack: JhaoProto.req_server_internal_createRoom = ProtoManager.instance.decode(JhaoProto.req_server_internal_createRoom, socketMsg.data)
        RoomModel.roomMap.set(protoPack.room.roomId, protoPack.room)
        let protoResp: JhaoProto.Iresp_server_internal_createRoom = {
        }
        conn.createMsgToClient(socketMsg.cmdIndex, socketMsg.cmdOrder, JhaoProto.resp_server_internal_createRoom, protoResp)
    }










}

